package com.mobisys.live.util;

import com.mobisys.live.dao.mapper.PictureMapper;
import com.mobisys.live.dao.model.Picture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PictureUtil {


    /**
     * 往图片表里插入url，并返回pictureId，如果插入失败则返回null
     * @param url
     * @return
     */
    public static String saveImgAndGetPicureId(String url,PictureMapper pictureMapper){
        Picture picture=new Picture();
        String pictureId=UUIDUtil.getUUID();
        picture.setId(pictureId);
        picture.setOriginal(url);
        if(pictureMapper.insertSelective(picture)>0){
            return pictureId;
        }else {
            return null;
        }
    }

    /**
     * 使用pictureId去获取图片的url，如果获取失败则返回null
     * @param id
     * @return
     */
    public static String getPictureUrlFromId(String id,PictureMapper pictureMapper){
        Picture picture=pictureMapper.selectByPrimaryKey(id);
        if(picture!=null){
            return picture.getOriginal();
        }
        return null;
    }


}
